/**
 * 对WebSocket进行jQuery的封装，方便使用 author：Mark cheng V1.0.0
 */
(function($) {
	$.extend({
		webSocketSettings : {
			open : function() {
				console.log("websocket 连接成功");
			},
			close : function() {
				console.log("websocket 已关闭连接");
			},
			message : function() {
			},
			error : function() {
				console.log("websocket 连接失败")
			},
			options : {},
			events : {}
		},
		webSocket : function(wsurl, protocols, settings) {
			jQuery.extend($.webSocketSettings, settings);
			var websocket = null
			// 判断当前浏览器是否支持WebSocket,引入Websocket重连包
			if ('WebSocket' in window) {
				websocket = new ReconnectingWebSocket(wsurl, null,
						settings.rcOptions);
			} else {
				console.log('Not support websocket');
				return null;
			}
			
			$(websocket).bind('open', $.webSocketSettings.open)
						.bind('close', $.webSocketSettings.close)
						.bind('message', $.webSocketSettings.message)
						.bind('error',$.webSocketSettings.error);
			websocket._send = websocket.send;
			websocket.send = function(type, data) {
				var m = {
					type : type
				};
				m = $.extend(true, m, $.extend(true, {},
						$.websocketSettings.options, m));
				if (data)
					m['data'] = data;
				return this._send(m);
				// 如果没有json插件就使用下面这行
				// return this._send(JSON.stringify(m));
			}
			$.webSocketSettings.close = function() {
				$.websocket.close;  
			}
			
			return websocket;

		}
	});
})(jQuery);